﻿var z = 0;
function initValid() {
    var x = parseInt(Math.random() * 10);
    var y = parseInt(Math.random() * 10);
    z = x + y;
    $("#valid_input").val("");
    $("#valid_input").attr("placeholder", x + "+" + y + "=");
}
$('#valid_input').click(function (e) {
    initValid();
});
initValid();
layui.define(['element', 'layer', 'form'], function (exports) {
    var form = layui.form;
    console.log(layer)
    var $ = layui.jquery;

    //自定义验证
    form.verify({
        passWord1: [/^[\S]{6,12}$/, '密码必须6到12位']
        , passWord: function (value) {
            var repassvalue = $('#password1').val();
            if (value != repassvalue) {
                return '两次输入的密码不一致!';
            }
        }
        ,
        account: function (value) {
            if (value.length <= 0 || value.length > 10) {
                return "账号必须1到10位"
            }
            var reg = /^[a-zA-Z0-9]*$/;
            if (!reg.test(value)) {
                return "账号只能为英文或数字";
            }
        }
        ,
        email: [/^[a-z0-9._%-]+@([a-z0-9-]+\.)+[a-z]{2,4}$|^1[3|4|5|7|8]\d{9}$/, '邮箱格式不对']
        ,
        result_response: function (value) {
            var rs = parseInt($("#valid_input").val());
            if (rs != z) {
                initValid();
                return "请输入正确的校验结果";
            }
        }
    })
    ;

    //监听登陆提交
    form.on('submit(register)', function (data) {
        var index = layer.load(1);
        console.log(data);
        setTimeout(function () {
            $.ajax({
                url: '../user/register',
                async: false,
                type: 'post',
                dateType: 'json',
                data: data.field,
                success: function (result) {
                    layer.close(index);
                    if (result.status == true) {
                        layer.msg('注册成功，正在跳转到登录界面......', {icon: 6});
                        layer.closeAll('page');
                        setTimeout(function () {
                            location.href = "login";
                        }, 1000);
                    } else {
                        layer.msg(result.msg, {icon: 5});
                    }
                }
            });
        }, 400);
        return false;
    });
    exports('register', {});
});

